/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package network.packets;

import datamodel.DriverState;
import utils.StringUtils;
import utils.Utils;

/**
 *
 * @author Kvest
 */
public class ChangeDriverStateFromServer extends Packet
{
    private DriverState state;
    private DriverState nextState;
    private int functionsMask;
    
    public ChangeDriverStateFromServer(byte[] data)
    {
        super(CHANGE_DRIVER_STATE_FROM_SERVER, data);
    }
    
    protected void parse(byte[] data) 
    {        
        int offset = 2;
        
        //Текущее состояние
        byte[] buffer = new byte[2];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        int stateId = Utils.byteToInt(buffer);
        
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        int stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        String stateName = StringUtils.bytesToStr(buffer);
        
        state = new DriverState(stateId, stateName);
        
        //Следующее состояние водителя
        buffer = new byte[2];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stateId = Utils.byteToInt(buffer);
        
        buffer = new byte[4];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stringSize = Utils.byteToInt(buffer);
        buffer = new byte[stringSize];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        stateName = StringUtils.bytesToStr(buffer);
        
        nextState = new DriverState(stateId, stateName);
        
        //Маска "Функции"
        buffer = new byte[2];
        System.arraycopy(data, offset, buffer, 0, buffer.length);
        offset += buffer.length;
        functionsMask = Utils.byteToInt(buffer);
    }
    
    public void print() 
    {
        System.out.println("ChangeDriverStateResponce[state=" + state.toString() + ", nextState=" + nextState.toString()  + ", functionsMask=" + functionsMask + "]");
    }

    public DriverState getState() 
    {
        return state;
    }

    public DriverState getNextState() 
    {
        return nextState;
    }

    public int getFunctionsMask() 
    {
        return functionsMask;
    }    
}
